package com.ken.work.apache.mina;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.stream.ImageInputStream;

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.http.api.DefaultHttpResponse;
import org.apache.mina.http.api.HttpResponse;
import org.apache.mina.http.api.HttpStatus;
import org.apache.mina.http.api.HttpVersion;

import com.ken.work.apache.MinaManager;
import com.ken.work.util.StringUtil;

public class MyIoHandlerAdapter extends IoHandlerAdapter {
	private static Logger log = Logger.getLogger(HttpServerHandler.class);

	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		 

	}

	@Override
	public void messageSent(IoSession session, Object arg1) throws Exception {
		// TODO Auto-generated method stub
		 
	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		log.info("服务端与客户端创建关闭..." + arg0.getRemoteAddress().toString() + "...."
				+ Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + ":"
				+ Calendar.getInstance().get(Calendar.MINUTE) + ":"
				+ Calendar.getInstance().get(Calendar.SECOND));
		 
	}

	@Override
	public void sessionCreated(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
 		log.info("服务端与客户端创建连接..." + arg0.getRemoteAddress().toString() + "...."
				+ Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + ":"
				+ Calendar.getInstance().get(Calendar.MINUTE) + ":"
				+ Calendar.getInstance().get(Calendar.SECOND));
		 
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionOpened(IoSession arg0) throws Exception {
	 
	}

	public HttpResponse getNullResponse()
	{
		//byte[] imgBytes =StringUtil.readBytes(new File(MinaManager.currFilePath+"/favicon.ico"));
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "text/html; charset=utf-8");
        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers);
        return response;
	}
	public MyHttpResponse getHtmlResponse(String requestPath)
	{
		MyHttpResponse myHttpResponse=new MyHttpResponse();
		String filePahtString=(MinaManager.currFilePath+requestPath).replace("//", "/");
		String htmlStrs =StringUtil.readBytes(filePahtString);
		System.out.println("文件路径:"+filePahtString);
		if(StringUtil.isEmpty(htmlStrs))
		{
		    String responseHtml = "<html><body>NOT FOUND ----"+requestPath+"</body></html>";
	        byte[] responseBytes = null;
			try {
				responseBytes = responseHtml.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        int contentLength = responseBytes.length;
	        
	        Map<String, String> headers = new HashMap<String, String>();
	        headers.put("Content-Type", "text/html; charset=utf-8");
	        headers.put("Content-Length", Integer.toString(contentLength));
	        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.CLIENT_ERROR_NOT_FOUND, headers);
	        
	        IoBuffer responseIoBuffer = IoBuffer.allocate(contentLength);
	        responseIoBuffer.put(responseBytes);
	        responseIoBuffer.flip();
	        myHttpResponse.setIoBuffer(responseIoBuffer);
	        myHttpResponse.setResponse(response);
	        return myHttpResponse;
		}
		else {
			 byte[] responseBytes = null;
				try {
					responseBytes = htmlStrs.getBytes("UTF-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        int contentLength = responseBytes.length;
	        
	        Map<String, String> headers = new HashMap<String, String>();
	        headers.put("Content-Type", "text/html; charset=utf-8");
	        headers.put("Content-Length", Integer.toString(contentLength));
	        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers);
	        
	        IoBuffer responseIoBuffer = IoBuffer.allocate(contentLength);
	        responseIoBuffer.put(responseBytes);
	        responseIoBuffer.flip();
	        myHttpResponse.setIoBuffer(responseIoBuffer);
	        myHttpResponse.setResponse(response);
	        return myHttpResponse;
		}
		
       
        
	}
	
}
